import store from '@/store/index.js'
// 必须以“/”结尾
// const baseUrl = 'http://lhva2467.nat1.z100.vip/';
const baseUrl = 'http://localhost:8080/';

// 排除不判断401的接口地址
let PermitAllUrlProperties = [
    '/wechat/mini-login',
    '/login'
]

const request = options => {
    let token = store.state.user.token
    // console.log('mapState ->', token)
    if (options && options.header) {
        if (token) {
            options.header['Authorization'] = `Bearer ${token}`;
        }
    } else {
        if (token)
            options.header = {
                Authorization: `Bearer ${token}`
            }
    }
    // console.log('options.url -> ', options.url);
    return new Promise((resolve, reject) => {
        uni.request(options).then(({ data }) => {
            if (data.code === 401) {
                uni.redirectTo({
                    url: '/pages/login/index'
                });
            } else if (data.code === 200) {
                resolve(data)
            } else {
                reject(data)
            }
        })
    })
}

export function get(url, data) {
    return request({
        url: `${baseUrl}${url}`, //仅为示例，并非真实接口地址。
        data: data,
        method: 'GET'
    })
}

export function post(url, data) {
    return request({
        url: `${baseUrl}${url}`, //仅为示例，并非真实接口地址。
        data: data,
        method: 'POST'
    })
}

export default {
    get, post
}